<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('代码生产列表')"/>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form action="" class=" layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">表名</label>
                            <div class="layui-input-inline">
                                <input class="layui-input" name="tableName" placeholder="请输入表名" type="text">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-md" lay-filter="gen-query" lay-submit>
                                <i class="layui-icon layui-icon-search"></i>
                                查询
                            </button>
                            <button class="layui-btn layui-btn-md layui-btn-primary" type="reset">
                                <i class="layui-icon layui-icon-refresh"></i>
                                重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <table id="gen-table" lay-filter="gen-table"></table>
    </div>
</div>
</body>

<script id="gen-toolbar" type="text/html">
    <button class="layui-btn layui-btn-md layui-btn-primary" lay-event="batch-gencode"><i class="layui-icon">&#xe654;</i>生成
    </button>
    <button class="layui-btn layui-btn-md" lay-event="import"><i class="layui-icon">&#xe654;</i>导入
    </button>
    <button class="layui-btn layui-btn-md layui-btn-danger" lay-event="batch-remove"><i class="layui-icon">&#xe640;</i>删除
    </button>
</script>
<script id="col_operation" type="text/html">
    <a href="javascript:;" lay-event="preview" class="layui-btn layui-btn-sm ">
        <i class="layui-icon layui-icon-about"></i>
    </a>
    <a href="javascript:;" lay-event="edit" class="layui-btn layui-btn-sm layui-btn-primary">
        <i class="layui-icon layui-icon-edit"></i>
    </a>
    <a href="javascript:;" lay-event="gencode" class="layui-btn layui-btn-sm layui-btn-success">
        <i class="layui-icon layui-icon-export"></i>
    </a>
</script>
<th:block th:include="include :: footer"/>
<script>
    layui.use(function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let prefix = pName + "/system/gen/";

        let cols = [
            [{
                type: 'checkbox'
            }, {
                field: 'tableName',
                title: '表名称'
            }, {
                field: 'tableComment',
                title: '表描述'
            }, {
                field: 'className',
                title: '实体类名称'
            }, {
                field: 'createTime',
                title: '创建时间',
                width: 160,
                sort: true
            }, {
                field: 'updateTime',
                title: '更新时间',
                width: 160
            }, {
                title: '操作',
                align: 'center',
                width: 220,
                toolbar: '#col_operation'
            }]
        ]

        table.render({
            elem: '#gen-table',
            url: prefix + 'data',
            page: true,
            escape: true,
            cols: cols,
            skin: 'line',
            toolbar: '#gen-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            ,height: 'full-135'
        });

        table.on('checkbox(gen-table)', function(obj){

        });
        table.on('row(gen-table)', function (obj) {

        });
        table.on('tool(gen-table)', function (obj) {
            switch (obj.event) {
                case 'preview':
                    let preViewUrl = prefix + "preview/" + obj.data.tableId;
                    layui.layer.load(1, {
                        shade: [0.2, '#fff']
                    });
                    $.get(preViewUrl, function (result) {
                        layui.layer.closeAll();
                        if (result.code == 1) {
                            let items = [];
                            $.each(result.data, function (index, value) {
                                value = value.replace(/</g, "&lt;");
                                value = value.replace(/>/g, "&gt;");
                                let templateName = index.substring(index.lastIndexOf("/") + 1, index.length).replace(/\.vm/g, "");
                                items.push({
                                    title: templateName,
                                    content: "<pre class=\"layui-code\">" + value + "</pre><style>.layui-layer-content {width: 100%;}.layui-layer-tabmain {margin-top: -20px;}</style>"
                                })
                            });
                            top.layer.tab({
                                area: ['99%', '99%'],
                                shadeClose: true,
                                tab: items
                            });
                        } else {
                            layui.layer.alert(result.msg);
                        }
                    });
                    break;
                case 'gencode':
                    let tableName = obj.data.tableName;
                    let genType = obj.data.genType;
                    layer.confirm('确定要生成' + tableName + '表代码吗？', function (index) {
                        if (genType === "0") {
                            location.href = prefix + "download/" + tableName;
                            layer.msg('执行成功,正在生成代码请稍后…', {
                                icon: 1
                            });
                        } else if (genType === "1") {
                            $.get(prefix + "genCode/" + tableName);
                            layer.msg('执行成功,请稍后刷新自定义代码生成目录…', {
                                icon: 1
                            });
                        }
                    });
                    break;
                case 'edit':
                    let url = prefix + 'edit?tableId=' + obj.data.tableId;
                    location.href = url;
                    break;
            }
        });

        table.on('toolbar(gen-table)', function (obj) {
            switch (obj.event) {
                case 'batch-remove':
                    window.batchRemove(obj);
                    break;
                case 'batch-gencode':
                    var checks = table.checkStatus('gen-table');
                    if (checks.data.length == 0) {
                        layer.alert('请选择要生成的数据');
                        return;
                    }
                    layer.confirm('确认要生成选中的' + checks.data.length + '条数据吗?', function (index) {
                        let tables = common.joinArray(checks.data, "tableName");
                        location.href = prefix + "batchGenCode?tables=" + tables;
                        layer.msg('执行成功,正在生成代码请稍后…', {
                            icon: 1
                        });
                    });
                    break;
                case 'import':
                    let layerIndex = layer.open({
                        type: 2,
                        title: '导入表结构',
                        shade: false,
                        fixed: false,
                        area: ['99%', '99%'],
                        content: prefix + "add",
                        btn: ["保存", "取消"],
                        yes: function (index, layero) {
                            layero.find('iframe')[0].contentWindow.submitHandler($);
                            table.reload('gen-table');
                        },
                        cancel: function (index, layero) {
                            layui.layer.close(index);
                            return false;
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(layerIndex);
                    });
                    break;
            }
        });

        form.on('submit(gen-query)', function (data) {
            table.reload('gen-table', {page: {curr: 1}, where: data.field})
            return false;
        });

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = common.joinArray(data, "tableId");

            layer.confirm('确定要删除吗？', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('gen-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('gen-table');
        }

    })
</script>
</html>